package cn.xiehang.SoundSight;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class SettingFragment extends Fragment {
    Button logoutButton;
    View layout1;
    LinearLayout privacy_policy_layout;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_setting, container, false);
        layout1 = view.findViewById(R.id.account_info_layout);
        SharedPreferences sharedPref = requireActivity().getSharedPreferences("user_prefs", Context.MODE_PRIVATE);



        logoutButton = view.findViewById(R.id.logout_button);
        privacy_policy_layout = view.findViewById(R.id.privacy_policy_layout);
        privacy_policy_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理点击事件
                if (isLoggedIn()) {
                    // 显示服务条款政策
                    showTermsDialog();
                } else {
                    // 显示请先登录的提示
                    Toast.makeText(getActivity(), "请先登录", Toast.LENGTH_SHORT).show();
                }
            }
        });
        if (isLoggedIn()){
            // 显示注销按钮
            logoutButton.setText("注销登录");
        } else {
            // 隐藏注销按钮
            logoutButton.setText("返回登录界面");
        }

        logoutButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                logout();
            }
        });

        layout1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理点击事件
                if (isLoggedIn()) {
                    // 转跳到个人信息界面
                    // 获取宿主 Activity
                    FragmentActivity activity = getActivity();
                    if (activity != null) {
                        // 执行 Fragment 切换
                        FragmentManager fm=activity.getSupportFragmentManager();
                        FragmentTransaction transaction = fm.beginTransaction();
                        //隐藏所有的Fragment
                        for (Fragment frag : fm.getFragments()) {
                            if (frag != null && frag.isAdded()) {
                                transaction.hide(frag);
                            }
                        }
                        Fragment existingFragment = fm.findFragmentByTag("AccountSettingFragment");

                        if (existingFragment != null) {
                            transaction.show(existingFragment);
                        } else {
                            Fragment accountSettingFragment = new AccountSettingFragment();
                            transaction.add(R.id.FragmentContainer, accountSettingFragment, "AccountSettingFragment");
                        }
                        transaction.commit();
                    }

                } else {
                    // 显示请先登录的提示
                    Toast.makeText(getActivity(), "请先登录", Toast.LENGTH_SHORT).show();
                }
            }
        });



        return view;

    }
    private void showTermsDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View dialogView = getLayoutInflater().inflate(R.layout.dialog_terms, null);
        builder.setView(dialogView);

        AlertDialog dialog = builder.create();

        // 设置对话框大小
        dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);

        Button btnAccept = dialogView.findViewById(R.id.btnAccept);
        btnAccept.setOnClickListener(v -> {
            dialog.dismiss();
        });

        dialog.show();
    }

    public Boolean isLoggedIn() {
        SharedPreferences sharedPref = requireActivity().getSharedPreferences("user_prefs", Context.MODE_PRIVATE);
        return sharedPref.getBoolean("is_logged_in", false);
    }

    public void logout() {
        SharedPreferences sharedPref = requireActivity().getSharedPreferences("user_prefs", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.remove("username");
        editor.remove("password");
        editor.remove("is_logged_in");
        editor.apply();

        // 跳转回登录界面
        Intent intent = new Intent(requireActivity(), MainActivity.class);

        // 清除任务栈并创建新任务
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);

    }
    public static SettingFragment newInstance() {
        return new SettingFragment();
    }
}
